﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using CQIE.TES.DAL;
using CQIE.TES.IDAL;
using CQIE.TES.IService;
using CQIE.TES.Service;

namespace CQIE.TES.WEB.USER.App_Start
{
    public static class AutoFacInit
    {
        public static void Start()
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();

            builder.RegisterApiControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();

            builder.RegisterAssemblyTypes(typeof(StudentDAL).Assembly)
                .AsImplementedInterfaces()
                .InstancePerRequest()
                // 属性注入
                .PropertiesAutowired();

            builder.RegisterAssemblyTypes(typeof(StudentService).Assembly)
                .AsImplementedInterfaces()
                .InstancePerRequest();

            var container = builder.Build();

            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}